# Copyright (c) 2023-2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

header = """/*
 * Copyright (c) 2021-2024, NVIDIA CORPORATION.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * NOTE: this file is generated by registers_00_generate.py
 *
 * Make changes there and run in this directory:
 *
 * > python registers_00_generate.py
 *
 */

#include <cstdint> // int64_t
#include <raft/spatial/knn/detail/ball_cover/registers-inl.cuh>

"""


macro_pass_one = """
#define instantiate_raft_spatial_knn_detail_rbc_low_dim_pass_one(                            \\
  Mvalue_idx, Mvalue_t, Mvalue_int, Mmatrix_idx, Mdims, Mdist_func)                          \\
  template void                                                                              \\
  raft::spatial::knn::detail::rbc_low_dim_pass_one<Mvalue_idx, Mvalue_t, Mvalue_int, Mmatrix_idx, Mdims>( \\
    raft::resources const& handle,                                                           \\
    const BallCoverIndex<Mvalue_idx, Mvalue_t, Mvalue_int, Mmatrix_idx>& index,              \\
    const Mvalue_t* query,                                                                   \\
    const Mvalue_int n_query_rows,                                                           \\
    Mvalue_int k,                                                                            \\
    const Mvalue_idx* R_knn_inds,                                                            \\
    const Mvalue_t* R_knn_dists,                                                             \\
    Mdist_func<Mvalue_t, Mvalue_int>& dfunc,                                                 \\
    Mvalue_idx* inds,                                                                        \\
    Mvalue_t* dists,                                                                         \\
    float weight,                                                                            \\
    Mvalue_int* dists_counter)

"""

macro_pass_two = """
#define instantiate_raft_spatial_knn_detail_rbc_low_dim_pass_two(                            \\
  Mvalue_idx, Mvalue_t, Mvalue_int, Mmatrix_idx, Mdims, Mdist_func)                          \\
  template void                                                                              \\
  raft::spatial::knn::detail::rbc_low_dim_pass_two<Mvalue_idx, Mvalue_t, Mvalue_int, Mmatrix_idx, Mdims>( \\
    raft::resources const& handle,                                                           \\
    const BallCoverIndex<Mvalue_idx, Mvalue_t, Mvalue_int, Mmatrix_idx>& index,              \\
    const Mvalue_t* query,                                                                   \\
    const Mvalue_int n_query_rows,                                                           \\
    Mvalue_int k,                                                                            \\
    const Mvalue_idx* R_knn_inds,                                                            \\
    const Mvalue_t* R_knn_dists,                                                             \\
    Mdist_func<Mvalue_t, Mvalue_int>& dfunc,                                                 \\
    Mvalue_idx* inds,                                                                        \\
    Mvalue_t* dists,                                                                         \\
    float weight,                                                                            \\
    Mvalue_int* dists_counter)

"""

macro_pass_eps = """
#define instantiate_raft_spatial_knn_detail_rbc_eps_pass(                                  \\
  Mvalue_idx, Mvalue_t, Mvalue_int, Mmatrix_idx, Mdist_func)                               \\
  template void                                                                            \\
  raft::spatial::knn::detail::rbc_eps_pass<Mvalue_idx, Mvalue_t, Mvalue_int, Mmatrix_idx>( \\
    raft::resources const& handle,                                                         \\
    const BallCoverIndex<Mvalue_idx, Mvalue_t, Mvalue_int, Mmatrix_idx>& index,            \\
    const Mvalue_t* query,                                                                 \\
    const Mvalue_int n_query_rows,                                                         \\
    Mvalue_t eps,                                                                          \\
    const Mvalue_t* R_dists,                                                               \\
    Mdist_func<Mvalue_t, Mvalue_int>& dfunc,                                               \\
    bool* adj,                                                                             \\
    Mvalue_idx* vd);                                                                       \\
                                                                                           \\
  template void                                                                            \\
  raft::spatial::knn::detail::rbc_eps_pass<Mvalue_idx, Mvalue_t, Mvalue_int, Mmatrix_idx>( \\
    raft::resources const& handle,                                                         \\
    const BallCoverIndex<Mvalue_idx, Mvalue_t, Mvalue_int, Mmatrix_idx>& index,            \\
    const Mvalue_t* query,                                                                 \\
    const Mvalue_int n_query_rows,                                                         \\
    Mvalue_t eps,                                                                          \\
    Mvalue_int* max_k,                                                                     \\
    const Mvalue_t* R_dists,                                                               \\
    Mdist_func<Mvalue_t, Mvalue_int>& dfunc,                                               \\
    Mvalue_idx* adj_ia,                                                                    \\
    Mvalue_idx* adj_ja,                                                                    \\
    Mvalue_idx* vd)

"""


distances = dict(
    haversine="raft::spatial::knn::detail::HaversineFunc",
    euclidean="raft::spatial::knn::detail::EuclideanFunc",
    dist="raft::spatial::knn::detail::DistFunc",
)

euclideanSq="raft::spatial::knn::detail::EuclideanSqFunc",

types = dict(
    int64_float=("std::int64_t", "float"),
    #int64_double=("std::int64_t", "double"),
)

for k, v in distances.items():
    for dim in [2, 3]:
        path = f"registers_pass_one_{dim}d_{k}.cu"
        with open(path, "w") as f:
            f.write(header)
            f.write(macro_pass_one)
            for type_path, (int_t, data_t) in types.items():
                f.write(f"instantiate_raft_spatial_knn_detail_rbc_low_dim_pass_one(\n")
                f.write(f"  {int_t}, {data_t}, {int_t}, {int_t}, {dim}, {v});\n")
            f.write("#undef instantiate_raft_spatial_knn_detail_rbc_low_dim_pass_one\n")
        print(f"src/spatial/knn/detail/ball_cover/{path}")

for k, v in distances.items():
    for dim in [2, 3]:
        path = f"registers_pass_two_{dim}d_{k}.cu"
        with open(path, "w") as f:
            f.write(header)
            f.write(macro_pass_two)
            for type_path, (int_t, data_t) in types.items():
                f.write(f"instantiate_raft_spatial_knn_detail_rbc_low_dim_pass_two(\n")
                f.write(f"  {int_t}, {data_t}, {int_t}, {int_t}, {dim}, {v});\n")
            f.write("#undef instantiate_raft_spatial_knn_detail_rbc_low_dim_pass_two\n")
        print(f"src/spatial/knn/detail/ball_cover/{path}")

path="registers_eps_pass_euclidean.cu"
with open(path, "w") as f:
    f.write(header)
    f.write(macro_pass_eps)
    for type_path, (int_t, data_t) in types.items():
            f.write(f"instantiate_raft_spatial_knn_detail_rbc_eps_pass(\n")
            f.write(f"  {int_t}, {data_t}, {int_t}, {int_t}, {euclideanSq});\n")
    f.write("#undef instantiate_raft_spatial_knn_detail_rbc_eps_pass\n")
    print(f"src/spatial/knn/detail/ball_cover/{path}")

